
package com.ifcar.app.activity;

import com.ifcar.app.activity.newspage.NewsCommentFragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

import java.util.ArrayList;

/**
 * 新闻正文页面
 * 
 * @author xali
 */
public class NewsPage extends CommonFrameActivity {

    public void onCreate(Bundle icicle) {
        createParam();
        super.onCreate(icicle);

        MyApplication myapplication = (MyApplication) getApplicationContext();
        if (null != myapplication) {
            myapplication.getHistoryRelativeDoc().add(this);
        }
    }

    private void createParam() {
        Intent intent = getIntent();

        Bundle args = new Bundle();
        if (intent.getBundleExtra(NewsCommentFragment.PARAM_FOR_NEWS) != null) {
            args.putBundle(NewsCommentFragment.PARAM_FOR_NEWS,
                    intent.getBundleExtra(NewsCommentFragment.PARAM_FOR_NEWS));
        } else {
            args.putBundle(NewsCommentFragment.PARAM_FOR_NEWS, intent.getExtras());
        }

        args.putBundle(NewsCommentFragment.PARAM_FOR_COMMENT,
                intent.getBundleExtra(NewsCommentFragment.PARAM_FOR_COMMENT));
        args.putBoolean(NewsCommentFragment.PARAM_FROM_COMMENT,
                intent.getBooleanExtra(NewsCommentFragment.PARAM_FROM_COMMENT, false));
        args.putBoolean(NewsCommentFragment.PARAM_FOR_GO_MAIN,
                intent.getBooleanExtra(NewsCommentFragment.PARAM_FOR_GO_MAIN, false));

        Bundle extra = new Bundle();
        extra.putInt(EXTRA_PARAM_LAYOUT, 0);
        extra.putInt(EXTRA_PARAM_CONTAINER, 0);

        Bundle param = new Bundle();
        param.putString(FRAGMENT_NAME, NewsCommentFragment.class.getName());
        param.putString(FRAGMENT_TAG, "NewsCommentFragment");
        param.putBundle(FRAGMENT_ARGU, args);
        param.putBundle(EXTRA, extra);
        getIntent().putExtra(COMMON_FRAME_START_ME_PARAM, param);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (null != mFragment) {
            ((NewsCommentFragment) mFragment).dispatchNewsFragment();
        }
        return super.dispatchTouchEvent(ev);
    }

    public void finishAllNewsPage() {
        MyApplication myapplication = (MyApplication) getApplicationContext();
        if (null != myapplication) {
            ArrayList<Activity> historyRelativeDoc = new ArrayList<Activity>();
            historyRelativeDoc.addAll(myapplication.getHistoryRelativeDoc());
            for (Activity a : historyRelativeDoc) {
                a.finish();
            }
            myapplication.getHistoryRelativeDoc().clear();
        }
    }
    
    public void finishNewsPage() {
        finish();
        
        MyApplication myapplication = (MyApplication) getApplicationContext();
        if (null != myapplication) {
            myapplication.getHistoryRelativeDoc().remove(this);
        }
    }

}
